$(document).ready(function() {
	// 行条纹效果
	$('#chart tbody tr:nth-child(even)').addClass('alt');
	// 输入掩码
	$('td.quantity input').keypress(function(event) {
		// console.log(event);return;
		if (event.which && (event.which < 48 || event.which > 57))
		{
			event.preventDefault();
		}
	});
	
	// 数字计算 解析和格式化货币值
	$('td.quantity input').change(function() {
		var totalQuantity = 0;
		var totalCost = 0;
		$('#chart tbody tr').each(function() {
			var price = parseFloat($('td.price', this).text().replace(/^[^\d.]*/,''));
			price = isNaN(price) ? 0 : price;
			var quantity = parseInt($('td.quantity input', this).val());
			var cost = quantity * price;
			$('td.cost', this).text('$' + cost.toFixed(2));
			totalQuantity += quantity;
			totalCost += cost;
			
		});
		var taxRate = parseFloat($('tr.tax td.price').text()) / 100;
		var tax = Math.ceil(totalCost * taxRate * 100) / 100;
		$('tr.tax td.cost').text('$' + tax.toFixed(2));
		totalCost += tax;
		var shippingRate = parseFloat($('tr.shipping td.price').text().replace(/^[^\d.]*/, ''));
		var shipping = totalQuantity + shippingRate;
		$('tr.shipping td.cost').text('$' + shipping.toFixed(2));
		totalCost += shipping;
		$('tr.total td.cost').text('$' + totalCost.toFixed(2));

		// console.log(totalQuantity);return;
		$('tr.shipping td.quantity').text(String(totalQuantity));
		$('tr.subtotal td.cost').text('$' + totalCost.toFixed(2));
	});
	$('#recalculate').hide();
});